﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_Flow_SignInDoc_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_Flow_SignInDoc_V2];
GO
CREATE PROCEDURE [dbo].[sproc_Flow_SignInDoc_V2]
    @StaffName nvarchar(300),
    @DocID int
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;

DECLARE @staff_id int;
DECLARE @PassNum int;

-- Get the id of the staff
SELECT @staff_id = staff_id FROM dbo.uds_staff WHERE staff_name = @StaffName;
IF @@rowcount <> 1
BEGIN
	raiserror(N'The specified user "%s" cannot be found.', 16, 1, @StaffName);
    return -1;
END;

BEGIN TRANSACTION;

--得到汇签规则
SELECT  @PassNum = step.PassNum 
    FROM 
        dbo.uds_flow_status s
        INNER JOIN dbo.uds_flow_document d ON s.doc_id = d.doc_id
        INNER JOIN dbo.uds_flow_step step ON (step.flow_id = d.flow_id AND step.step_id = d.step_id)
    WHERE
        s.Doc_ID = @docid;

IF @PassNum = 0
BEGIN
    --没有汇签,更新文档到签收待批状态
    UPDATE dbo.uds_flow_document SET doc_status = 1 WHERE doc_id = @DocID;
END
ELSE
BEGIN
    --多人汇签，更新文档到会签状态
    UPDATE dbo.uds_flow_document SET doc_status = 2 WHERE doc_id = @DocID;
END

--刷新对文档的状态
UPDATE dbo.uds_flow_status SET status = 1 WHERE doc_id = @DocID and staff_id = @staff_id;

COMMIT TRANSACTION;
RETURN 0

END
GO
